package net.sunbeta.test.guice;

import java.lang.reflect.Constructor;

import org.testng.IObjectFactory;
import org.testng.internal.ObjectFactoryImpl;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;

public class GuiceObjectFactory implements IObjectFactory {
	private Injector injector;
	private ObjectFactoryImpl creator = new ObjectFactoryImpl();

	public void setCurrentModule(Module module) {
		injector = Guice.createInjector(module);
	}

	@Override
	public Object newInstance(Constructor constructor, Object... objects) {
		Object o = creator.newInstance(constructor, objects);
		injector.injectMembers(o);
		return o;
	}

}
